// 获取应用实例
const app = getApp()

Page({
	data: {
		uid: "c124bafd03ba4909b1f7794c3e38af32", //用户密钥，巴法云控制台获取
		myTopic: "mytrash", //图片上传的主题，图片云控制台创建
		num: 1, //获取的图片数量，可随意
		imgList: [], //存储图片地址和时间，用于前端展示
		picArr: [], //存储图片的地址，用于图片点击预览
		overdata: false, //溢出状态
		online: false,//在线状态
		author: app.globalData.author,
  },

  getinfo1() {//获取设备在线状态
		var that = this
		wx.request({
			url: "https://apis.bemfa.com/va/online",
			method: "GET",
			data: {
				"uid": "c124bafd03ba4909b1f7794c3e38af32",
				"topic": "mytrash",
				"type": "3",
			},
			success: function (e) {
				//console.log("获取成功", e)
				that.setData({
					online:e.data.data,
				})
				//console.log("shebei:" + that.data.online);
			}
		});
  },
  
	getinfo2() {//获取垃圾桶相关信息
		var that = this
		wx.request({
			url: "https://apis.bemfa.com/va/getmsg",
			method: "GET",
			data: {
				"uid": "c124bafd03ba4909b1f7794c3e38af32",
				"topic": "mytrash",
				"type": "3",
				"num": "1"
			},
			success: function (e) {
				//console.log("获取成功", e)
				var sentence = e.data.data[0].msg;
				var matches = [];
				// 使用正则表达式查找"#"后面的内容
				var regex = /#([\w\s]+)/g;
				var match;
				while ((match = regex.exec(sentence)) !== null) {
					matches.push(match[1]);
        }
        //console.log(matches);
        if(matches[0] == "true")
          that.setData({overdata: true});
        else
          that.setData({overdata: false});
				that.setData({
					tempdata: matches[1],
					humidata: matches[2]
				})
			}
		});
	},

  getPicture() { //获取图片函数
		//api 接口详细说明见巴法云接入文档
		var that = this
		wx.request({
			url: 'https://images.bemfa.com/cloud/v1/get/', //获取图片接口，详见巴法云接入文档
			data: {
				uid: that.data.uid, //uid字段
				topic: that.data.myTopic, //topic字段
				num: that.data.num //num字段
			},
			header: {
				'content-type': "application/x-www-form-urlencoded"
			},
			success(res) {
				//console.log(res) //打印获取结果
				var imgArr = [] //定义空数组，用于临时存储图片地址和时间
				var arr = [] //定义空数组，用于临时存储图片地址
				for (var i = 0; i < res.data.data.length; i++) { //遍历获取的结果数组
					var url = res.data.data[i].url //提取图片地址
					var time = that.time(url.substring(url.lastIndexOf("-") + 1, url.lastIndexOf("."))) //提取图时间
					imgArr.push({
						"url": url,
						"time": time
					}) //将存储图片地址和时间存入临时数组
					arr.push(url) //将存储图片地址存入临时数组
				}

				that.setData({ //把临时数组值复制给变量用于展示
					imgList: imgArr, //将临时存储图片地址和图片时间的数组赋值给用于图片预览的数组
					picArr: arr, //将临时存储图片地址的数组赋值给用于图片预览的数组
				})
				//console.log(that.data.imgList) //打印赋值结果
			}
		})
  },
  
	onLoad() { //默认加载函数
		this.getPicture() //获取图片
		var that = this
		setInterval(function () {
      that.getPicture(),
			that.getinfo1(),
			that.getinfo2()
		}, 3000)
	},
	
	clickImg(e) { //点击预览函数
		//console.log(e.currentTarget.dataset.index) //打印数组索引值
		var nowIndex = e.currentTarget.dataset.index //获取索引值
		wx.previewImage({ //图片预览接口
			current: this.data.picArr[nowIndex], //当前图片地址
			urls: this.data.picArr //图片地址数组
		})
	},
	time(time) { //时间戳转时间函数
		var date = new Date(parseInt(time) * 1000 + 8 * 3600 * 1000);
		return date.toJSON().substr(0, 19).replace('T', ' ');
	},
})